export default class Cache {
  private data: Map<string, any> = new Map()

  has(key: string): boolean {
    return this.data.has(key)
  }

  /**
   * 获取
   * @param key
   * @param defaultValue
   */
  get<T extends any>(key: string, defaultValue?: () => T): T {
    if (this.data.has(key)) {
      return this.data.get(key)
    }
    const value = defaultValue()
    this.data.set(key, value)
    return value
  }

  /**
   * 设置
   * @param key
   * @param value
   */
  set<T>(key: string, value: T) {
    this.data.set(key, value)
  }

  /**
   * 删除
   * @param key
   */
  delete(key: string) {
    this.data.delete(key)
  }

  /**
   * 清空
   */
  clear() {
    this.data.clear()
  }
}
